// pages/dynamic/pushDynamic/pushDynamic.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    proImgArr:[],
    isAgree: false,//是否同意规则
    imgPath:[],//图片数组
    postData: {},//请求数据
  },

  //规则点击
  rulesClick:function(){
    console.log('规则点击');

    wx.navigateTo({
      url: '../../rules/rules' ,
    })
  },


  //手机输入
  phoneInput:function(e){
    var that = this;
    console.log(e.detail.value);
    var data = that.data.postData;
    data['contact_tel'] = e.detail.value;
    that.setData({
      postData: data,
    })
  },

  //内容输入
  textInput:function(e){
    var that = this;
    console.log(e.detail.value);
    var data = that.data.postData;
    data['content'] = e.detail.value;
    that.setData({
      postData: data,
    })
  },

  //选择产品9张
  editProductImg: function () {
    var that = this;
    wx.chooseImage({
      count: 9,
      success: function (res) {
        console.log(res.tempFilePaths);
        that.setData({
          proImgArr: res.tempFilePaths,
        })
      },
    })
  },

  //是否同意规则
  rulesAgree: function () {
    var that = this;
    that.setData({
      isAgree: !that.data.isAgree,
    })
  },

  //发布点击
  pushClick:function(){
    var that = this;
    if (that.data.isAgree){
      //判断是否上传图片
      if (that.data.proImgArr.length >0){
        that.setData({
          imgPath: [],//每次发布 清空数组防止上传失败引起图片重复
        })
        wx.showLoading({
          title: '正在上传图片',
        })
        that.uploadimg({
          url: app.globalData.requestURL + 'file/uploadImg',
          path: that.data.proImgArr,
        })
      }else{
        wx.showToast({
          title: '请选择图片',
          icon: 'none'
        })
      }
    }else{
      wx.showToast({
        title: '请先阅读并同意相关条款',
        icon: 'none'
      })
    }
   
  },

  //多张图片上传
  uploadimg: function (data) {
    console.log('------------');
    console.log(data.path);
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      header: {
        compress:'1',
        height:'108',
        width:'100',
      },
      name: 'fileData',
      formData: null,
      success: (resp) => {
        success++;
        console.log(resp.data)
        console.log(i);
        var result = JSON.parse(resp.data);
        if (result.code == 0){
          that.data.imgPath.push(result.resultData);
        }
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;
        if (i == data.path.length) { //当图片传完时，停止调用   
          console.log('执行完毕');
          wx.hideLoading();
          console.log('成功：' + success + " 失败：" + fail);
          //调用上传数据
          console.log(that.data.imgPath);
          that.requestData();//上传数据
        } else {//若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },

  //请求数据
  requestData:function(){
    var that = this;
    var data = that.data.postData;
    data['imgUrl'] = that.data.imgPath.join(",");
    that.setData({
      postData: data,
    })
    if (that.data.postData.contact_tel.length != 11) {
      wx.showToast({
        title: '手机号有误，请修改',
        icon: 'none',
      })
      return;
    }
    app.post('act/addActives', that.data.postData).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        wx.navigateBack();//返回上一页
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var data = that.data.postData;
    data['openId'] = wx.getStorageSync('userInfo').userId;
    that.setData({
      postData: data,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})